//
//  LZCacheTool.m
//  LiZhi
//
//  Created by wdy on 2017/3/6.
//  Copyright © 2017年 zsz. All rights reserved.
//

#import "LZCacheTool.h"
#import "YYCache.h"
#define kCachePath ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject])  //沙盒document路径
#define KCompanyCache @"ECCompanyCache"
@implementation LZCacheTool

static YYCache *cache;
+ (void)cacheForData:(id)data fileName:(NSString *)fileName
{
    [[LZCacheTool alloc]initYYCache];
    //异步缓存
    [cache setObject:data forKey:fileName withBlock:^{
        NSLog(@"缓存成功");
    }];
}

+ (id)getCacheFileName:(NSString *)fileName
{
    [[LZCacheTool alloc]initYYCache];
    BOOL isContains=[cache containsObjectForKey:fileName];
     NSLog(@"containsObject : %@", isContains?@"YES":@"NO");
    if (isContains) {
        //同步读取
        return  [cache objectForKey:fileName];;
    }else{
        return nil;
    }
}

-(void)initYYCache
{
    cache=[[YYCache alloc]initWithName:KCompanyCache];
    cache.memoryCache.shouldRemoveAllObjectsOnMemoryWarning=YES;
    cache.memoryCache.shouldRemoveAllObjectsWhenEnteringBackground=YES;
    
    cache.memoryCache.countLimit=50;    //内存最大缓存数据个数
//    cache.memoryCache.costLimit=50*1024*1024;//内存最大缓存开销
    cache.diskCache.costLimit=100*1024*1024;//磁盘最大开销
    cache.diskCache.countLimit=120;//最大缓存数据个数
    cache.diskCache.autoTrimInterval=60;//磁盘lru动态清理频率 默认 60秒
    
}

+ (void)clearCache
{
    [[LZCacheTool alloc]initYYCache];
    [cache removeAllObjectsWithBlock:^{
        NSLog(@"清除成功");
    }];
}
@end
